<template>
  <base-layout>
    <template #searchPanel>
      <search-panel
        v-slot="{ form }"
        :addPer="'storage:save'"
        :selectPer="'storage:page'"
        addTitle="新增仓库"
        @insert="add"
        @search="search"
        :showSearchButton="true"
        :showAddButton="true"
      >
        <job-shop v-model="form.storeId" placeholder="所属门店" />
        <el-select
          clearable
          style="width: 200px"
          v-model="form.storageType"
          placeholder="仓库类别"
        >
          <el-option
            v-for="item in dictArray['仓库类别']"
            :key="item.index"
            :value="item.value"
            :label="item.text"
          />
        </el-select>
        <CitySelect v-model="form.cityCode" />
        <el-input
          v-model="form.storageCodeAndName"
          clearable
          placeholder="仓库编号/仓库名称"
          style="width: 200px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
      >
        <template #action="{ row }">
          <i
            class="el-icon-edit"
            title="编辑"
            style="cursor: pointer"
            v-permission="'storage:update'"
            @click="handleDataUpdateEdit(row, '编辑')"
          ></i>
          <i
            class="el-icon-delete"
            title="删除"
            style="color: red; margin-left: 20px; cursor: pointer"
            v-permission="'storage:del'"
            @click="handleDataDelete(row)"
          ></i>
        </template>
      </data-table>
    </template>
  </base-layout>
</template>
<script>
import baseMixin from "@/mixins/base";
import {
  page,
  deleteById,
  openRestsStore,
} from "@/api/after-sale/storehouseManage/warehouseManage";
import { JobShop } from "@/components/index";
import CitySelect from "@/components/CitySelect/index";
import { getDict } from "@/utils";
export default {
  components: {
    JobShop,
    CitySelect,
  },
  mixins: [
    baseMixin({
      idName: "id",
      findData: page,
      dataName: ["content", "totalRecords"],
      format(res) {
        return res.map((data) => {
          data.preferred = data.preferred == true ? "是" : "否";
          data.openRestsStoreName = data.openRestsStore == true ? "启用" : "禁用";

          return data;
        });
      },
      deleteData: deleteById,
    }),
  ],
  data() {
    return {
      tableColumnAttributes: [
        { name: "所属门店", value: "storeName", type: "text" },
        { name: "仓库编号", value: "storageCode", type: "text" },
        { name: "仓库名称", value: "storageName", type: "text" },
        { name: "仓库类别", value: "storageType", type: "text" },
        { name: "仓库所在城市", value: "cityName", type: "text" },
        { name: "仓库所在区县", value: "countyName", type: "text" },
        { name: "仓库详情地址", value: "address", type: "text" },
        { name: "仓库管理员", value: "manageUserName", type: "text" },
        { name: "仓库联系电话", value: "storagePhone", type: "text" },
        { name: "仓库状态", value: "status", type: "text" },
        {
          name: "是否首选仓库",
          value: "preferred",
          type: "text",
        },
        {
          name: "是否开放给其他门店调拨",
          value: "openRestsStoreName",
          type: "switch",
          valueId: "openRestsStore",
          action: (row) => {
            openRestsStore(row.id).then((res) => {
              this.search();
            });
          },
        },
        { name: "仓库规模", value: "area", type: "text" },
        { name: "仓库备注", value: "remark", type: "text" },
        { name: "录入时间", value: "createTime", type: "text" },
      ],
      dictArray: [],
    };
  },
  methods: {
    add() {
      this.$router.push({ path: "/storehouseManage/addWarehouseManage" });
    },
    handleDataUpdateEdit(row, type) {
      this.$router.push({
        path: "/storehouseManage/editWarehouseManage",
        query: {
          id: row.id,
        },
      });
    },
    search(form) {
      this.searchOptions = form;
      this.currentPage = 1;
      this.setTableData();
    },
  },
  mounted() {
    this.dictArray = getDict(["仓库类别"]);
  },
};
</script>
